home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11795 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: qualcomm.com!not-for-mail
  2. From: drew@qualcomm.com (Drew Eckhardt)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Structure names are pointers? Writing to them?
  5. Date: 15 Mar 1996 23:55:52 -0700
  6. Organization: QUALCOMM, Incorporated; Boulder, CO, USA
  7. Message-ID: <4idolo$g92@qualcomm.com>
  8. References: <4idmdo$2b2@news.voicenet.com>
  9. NNTP-Posting-Host: littlebear.qualcomm.com
  10.  
  11. In article <4idmdo$2b2@news.voicenet.com>,  <deaton@cygnus.rsabbs.com> wrote:
  12. >blahS blah[15]; fwrite(blah,sizeof(blah),1,fptr);
  13.  
  14. works, as expected.
  15.  
  16. >blahS blah; fwrite(blah,sizeof(blah),1,fptr);
  17. >my compiler spits out an error. 
  18.  
  19. Be happy you're using a C++ compiler which flags this as a fatal error,
  20. rather than a 'C' compiler which may result in runtime nasal demons.
  21.  
  22. >As far as I understood, structure names acted the same way as array names: 
  23.  
  24. In both 'C' and C++, a conversion between an array and a pointer to the base 
  25. type exists, meaning an array name in a pointer context evaluates to 
  26. &array[0].
  27.  
  28. However, no conversion exists for structures, and would be undesirable 
  29. in C++ since it would preclude (or introduce some less intuitive syntax for)
  30. passing objects by value.
  31.  
  32. >they are pointers. 
  33.  
  34. They are not pointers; there is merely a conversion which allows them
  35. to act as pointers in certain useful places.  Thinking of them as pointers
  36. will get you in trouble.  For instance, you may try to use &array where 
  37. &pointer is required or vice versa.
  38.  
  39. >So why isn't this working? 
  40.  
  41. As noted, no such conversion exists for structures.   You want to use 
  42. this instead:
  43.  
  44.     fwrite (&blah, sizeof(blah), 1, fptr)
  45.  
  46.  
  47. -- 
  48. Four boxes : soap, ballot, jury, ammo.  | Work: drew@Qualcomm.COM       
  49. Use in that order.                      | Play: drew@PoohSticks.ORG    
  50.